//语音芯片NY3P065BS8

#include "defined.h"
#include "ny3p.h"
#include <dspDriver.h>
#include "ex_variable.h"
#include "Key.h"

u8 voice_fig, voice_mode[10];	//播放语音段的列表数组

void Player_NY3P_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

//变量赋初始
    voice_fig = 0;		//声音段数
    voice_mode[0] = 0;//声音组号

//配置复位和数据口线：输出	PB9
    GPIO_InitStructure.GPIO_Pin = NY3P_EN_PIN ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(NY3P_EN_PORT,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = NY3P_DATA_PIN ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(NY3P_DATA_PORT,&GPIO_InitStructure);
    NY3P_Reset_off();
    NY3P_Serial_off();
}

void Voice_Play(u8 s_id)
{
    u8 i;
    //2口线都设为低电平
    NY3P_Serial_off();
    NY3P_Reset_off();
    Delay_ms(1);
    //复位口线发送一个脉冲，表示开始播放新语音
    NY3P_Reset_on();
    Delay_us(100);
    NY3P_Reset_off();
    Delay_us(100);
    //数据口线发送N个脉冲，表示播放第N端语音
    for(i=0; i<s_id; i++)
    {
        NY3P_Serial_on();
        Delay_us(100);
        NY3P_Serial_off();
        Delay_us(100);
    }
    Delay_ms(1);
}

#define NEWVOICE

//播放：欢迎光临
void VoicePlay_HYGL(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    Voice_Play(3);
}

//播放：非法闯入
void VoicePlay_FFCR(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
#ifdef NEWVOICE
    Voice_Play(5);
#else
    Voice_Play(9);
#endif
//	Post_Info.Post_Alarm=ALARM_FFCR;
}
//播放：人员尾随
void VoicePlay_RYWS(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(8);
#ifdef NEWVOICE
    Voice_Play(6);
#else
    Voice_Play(8);
#endif
}

//播放：占用通道超时
void VoicePlay_ZYTDCS(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(6);
#ifdef NEWVOICE
    Voice_Play(5);
#else
    Voice_Play(6);
#endif
}
//播放：反向闯入
void VoicePlay_FXCR(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(7);
#ifdef NEWVOICE
    Voice_Play(5);
#else
    Voice_Play(7);
#endif
    //Post_Info.Post_Alarm=ALARM_FXCR;
}

//播放：小孩闯入
void VoicePlay_ETCR(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(10);
#ifdef NEWVOICE
    Voice_Play(5);
#else
    Voice_Play(10);
#endif
}

//播放：火警请撤离
void VoicePlay_HJQCL(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(11);
#ifdef NEWVOICE
    Voice_Play(7);
#else
    Voice_Play(11);
#endif
}

//播放：紧急关模式 无法进入
void VoicePlay_JJGMWFJR(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
//	Voice_Play(12);
#ifdef NEWVOICE
    Voice_Play(13);
#else
    Voice_Play(12);
#endif
//	Post_Info.Post_Alarm=ALARM_FFCR;
}

//播放：通行权限到期
void VoicePlay_TXQXDQ(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    Voice_Play(13);
}

//播放：验证成功  刷卡成功时
void VoicePlay_YZCG(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(14);
#ifdef NEWVOICE
    Voice_Play(8);
#else
    Voice_Play(14);
#endif
}

//播放：验证失败  刷卡失败时
void VoicePlay_YZSB(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(15);
#ifdef NEWVOICE
    Voice_Play(9);
#else
    Voice_Play(15);
#endif
}

//播放：设置成功
void VoicePlay_SZCG(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(30);
#ifdef NEWVOICE
    Voice_Play(11);
#else
    Voice_Play(30);
#endif
}

//播放：设置失败
void VoicePlay_SZSB(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(67);
#ifdef NEWVOICE
    Voice_Play(12);
#else
    Voice_Play(67);
#endif
}

//播放：恢复出厂成功
void VoicePlay_HFCCCG(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(56);
#ifdef NEWVOICE
    Voice_Play(10);
#else
    Voice_Play(56);
#endif
}


//播放：请刷卡
void VoicePlay_QSK(void)
{
    voice_fig = 0;
    voice_mode[0] = 0;
    //Voice_Play(52);
#ifdef NEWVOICE
    Voice_Play(58);
#else
    Voice_Play(52);
#endif
}

Play_Voice_Handle Play_Handle[] =
{
//以下单次触发
    {		VoicePlay_YZCG	},
    {		VoicePlay_YZSB	},
    {		VoicePlay_HFCCCG},
    {		VoicePlay_SZSB	},
    {		VoicePlay_SZCG	},
    {		VoicePlay_RYWS	},
    {		VoicePlay_FXCR	},
    {		VoicePlay_FXCR	},
    {		VoicePlay_HYGL},
//以下需屏蔽频繁触发和再播
    {		VoicePlay_ZYTDCS},
    {		VoicePlay_JJGMWFJR	},
    {		VoicePlay_HJQCL	}	,
    {		VoicePlay_FFCR	},
    {		VoicePlay_TXQXDQ	},
    {		VoicePlay_QSK	},
    {		VoicePlay_QSK}
};

//======================语音播放处理100ms=================================
void NY3P_Player_EVENT(void)
{
    u8 i;
    if(Param.Voice_Switch==0)  	return;	//语音播报屏蔽 直接返回
    if(Set_Center_StartFlag)  	return; //设置中点，检测到有报警不播报
    if(JJ_LowPower_Flag)				return; //掉电时防止红外报警-非法
    if(First_Player_Delay>0)  	return; //延时播报中

    if(G_Play_Again.cnt)
        G_Play_Again.cnt--;


    for(i=0; i<(sizeof(Play_Handle)/sizeof(Play_Handle[0])); i++)
    {
        if(((0x01<<i)&Param.Voice_Switch)==0)
        {   //未打开开关-跳过
            continue;
        }

        if(i<=8)
        {
            if(((0x01<<i)&G_Play.intData)!=0)
            {
                Play_Handle[i].VOICE_SEG();
                if((i==5)||(i==6)||(i==7))
                    G_PlayLast.intData|=(0x01<<i);//上一报警
                G_Play.intData&=~(0x01<<i);			//清对应报警标识
                break;
            }
        } else	if((i>8)&&(G_Play_Again.cnt==0))
        {
            if(((0x01<<i)&G_Play.intData)!=0)
            {
                Play_Handle[i].VOICE_SEG();
                if(i==12)
                    G_PlayLast.intData|=(0x01<<i);//上一报警
                G_Play_Again.cnt=Param.Play_Again_Tim;//置重复
                break;
            }
        }
    }
}

u8 Have_Alarm(u8 Alarmtype)
{
    if(((G_PlayLast.intData&(0x01<<Alarmtype))!=0)||((G_Play.intData&(0x01<<Alarmtype))!=0))
        return 1;
    else
        return 0;
}

//请对应报警
void ClrAlarm(u8 Alarmtype)
{
    G_PlayLast.intData&=~(0x01<<Alarmtype);
}


